package cn.community.system.web.controller;

import cn.community.system.domain.Association;
import cn.community.system.domain.Department;
import cn.community.system.query.DepartmentQuery;
import cn.community.system.query.QueryObject;
import cn.community.system.service.IAssociationService;
import cn.community.system.service.IDepartmentService;
import cn.community.system.util.JsonResult;
import cn.community.system.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RequestMapping("department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    // 提供一个方法处理查询所有部门数据请求，响应 HTML
    @RequestMapping("/list")
    //必须要有指定的权限才能访问该方法
    @RequiresPermissions(value = {"department/list","超管-部门列表"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo")DepartmentQuery qo){
        PageInfo pageInfo = departmentService.query(qo);
        model.addAttribute("page", pageInfo);
        return "department/list";
    }

    @RequestMapping("/listAsso")
    //必须要有指定的权限才能访问该方法
    @RequiresPermissions(value = {"department/listAsso","社团-部门列表"},logical = Logical.OR)
    public String listAsso(Model model, @ModelAttribute("qo")DepartmentQuery qo){
        PageInfo pageInfo = departmentService.queryAsso(qo);
        model.addAttribute("page", pageInfo);
        return "department/listAsso";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"department/saveOrUpdate","超管-部门新增/编辑"},logical = Logical.OR)
    public Object saveOrUpdate(Department department){
        if(department.getId() == null){ // 代表新增
            departmentService.save(department);
        }else { // 代表修改
            departmentService.update(department);
        }
        return JsonResult.success();
    }

    @RequestMapping("/saveOrUpdate2")
    @ResponseBody
    @RequiresPermissions(value = {"department/saveOrUpdate2","社团-部门新增/编辑"},logical = Logical.OR)
    public Object saveOrUpdate2(Department department){
        if(department.getId() == null){ // 代表新增
            departmentService.saveAsso(department);
        }else { // 代表修改
            departmentService.updateAsso(department);
        }
        return JsonResult.success();
    }


    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"department/delete","部门删除"},logical = Logical.OR)
    public JsonResult delete(Long id){
        departmentService.delete(id);
        return JsonResult.success();
    }

    @RequestMapping("/listDeptByAssoId")
    @ResponseBody
    public JsonResult listDeptByAssoId(Long id){
        Department department = departmentService.listDeptByAssoId(id);
        return JsonResult.success(department);
    }

    @RequestMapping("/listAll")
    @ResponseBody
    public JsonResult listAll(){
        Association association = UserContext.getCurrentAssociation();
        Department department = departmentService.listDeptByAssoId(association.getId());
        return JsonResult.success(department);
    }
}
